/**
 * Computer Network, Programming Assignment #2
 * 
 * FileSender Class
 * 
 * File sender application, use GPD's gSocket to send file transfer from a specific grid point
 * 
 * @author Yancheng Li
 *
 */

import java.io.*;
import java.net.Socket;


public class FileSender {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		
		
		DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
		String line = "";
		help();
		
		while(line.compareTo("exit") != 0){
			try {
				System.out.print("\n>>");
				
				line = in.readLine();
				
				String [] list = line.split(" ");
				
				if(list.length > 7){
					System.out.println("Error command");					
				}
										
				// to send a test packet from grid(0, 0) to grid(1, 2)
				
				if(list[0].compareTo("test") == 0){
					Socket socket = gSocket.connectTo(new Point(0, 0));
					PSPUtil.print("Test to send a packet to grid (1, 2) port 8000", true);					
					gSocket.sendPacket(socket, 0, 1, 8000, "test packet".toCharArray());
					socket.close();
					continue;
				}
				
				// to send a empty packet from grid(0, 0) to grid(1, 2)
				if(list[0].compareTo("end") == 0){
					Socket socket = gSocket.connectTo(new Point(0, 0));
					PSPUtil.print("Test to send final packet to grid (1, 2) port 8000", true);					
					gSocket.sendPacket(socket, 0, 1, 8000, "".toCharArray());
					socket.close();
					continue;
				}
				
				// print help info
				if(list[0].compareTo("help") == 0){
					help();
					continue;
				}
				
				// send a packet through grid(x1, y1) to grid(x2, y2)
				if(list[0].compareTo("send") == 0){
					int x1 = Integer.valueOf(list[1]);
					int y1 = Integer.valueOf(list[2]);
					
					int x2 = Integer.valueOf(list[3]);
					int y2 = Integer.valueOf(list[4]);
					
					int port = Integer.valueOf(list[5]);
					File inFile  = new File(list[6]);
					if(!inFile.exists()){
						PSPUtil.print("File not exist", true);
						continue;
					}
					PSPUtil.print("Start sending file", true);
					Socket socket = gSocket.connectTo(new Point(x1, y1));
					BufferedReader reader = new BufferedReader(new FileReader(inFile));
					char[] buf = new char[1024];
					while(true){
						int len = reader.read(buf, 0, 1024);
						if(len >= 1024){											
							gSocket.sendPacket(socket, x2, y2, port, buf);		
						}else if(len > 0){
							char[] newBuf = new char[len];
							for(int i = 0; i < len; i++)
								newBuf[i] = buf[i];
							
							gSocket.sendPacket(socket, x2, y2, port, newBuf);
							Thread.sleep(100);
							gSocket.sendPacket(socket, x2, y2, port, "".toCharArray());
							break;
						}else{
							gSocket.sendPacket(socket, x2, y2, port, "".toCharArray());
							break;
						}						
						
						
						Thread.sleep(100);						
					}
					socket.close();
					reader.close();
					PSPUtil.print("Sending file complete", true);
					continue;
				}
				PSPUtil.print("Error command", true);
				
				
			} catch (Exception e) {

				e.printStackTrace();
			}
		}								
	}
	
	public static void help(){
		System.out.println("======================================");
		System.out.println("Command List:");
		System.out.println("[1]test:\n send a test packet to grid (1, 2) on port 8000");
		System.out.println("[2]send:\n send src-x src-y dst-x dst-y dst-port fileToSend");		
		System.out.println("[3]help:\n print help info");
		System.out.println("======================================");
	}

}
